package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.ClueUserDao;
import com.xbongbong.saas.domain.entity.ClueUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.subform.ClueTeamEnum;
import com.xbongbong.saas.enums.subform.PaymentSheetTeamEnum;
import com.xbongbong.saas.model.ClueUserModel;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author long
 * @version v1.0
 * @date 2020/11/4 11:08
 * @since v1.0
 */
@Service("clueUserModel")
public class ClueUserModelImpl implements ClueUserModel {

    @Resource
    private ClueUserDao clueUserDao;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Override
    public Integer save(ClueUserEntity clueUserEntity) throws XbbException {
        if (clueUserEntity.getId() == null || clueUserEntity.getId().equals(0L)) {
            return insert(clueUserEntity);
        }
        return update(clueUserEntity);
    }

    @Override
    public ClueUserEntity getByKey(Long key, String corpid) {
        return clueUserDao.getByKey(key, corpid);
    }

    @Override
    public void insertBatch(List<ClueUserEntity> clueUserEntities) throws XbbException {
        insertBatch(clueUserEntities, WriteRequest.RefreshPolicy.NONE);
    }

    @Override
    public void insertBatch(List<ClueUserEntity> clueUserEntities, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        clueUserDao.insertBatch(clueUserEntities);
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_CLUE_TEAM, clueUserEntities);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CLUE_TEAM));
        }
        paasEsModel.insertBatch(insertDTOList, refreshPolicy);
    }

    @Override
    public Integer insert(ClueUserEntity entity) throws XbbException {
        Long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        Integer result = clueUserDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_CLUE_TEAM, entity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
        paasEsModel.insert(insertDTO);
        return result;
    }

    @Override
    public Integer update(ClueUserEntity entity) throws XbbException {
        Long now = DateTimeUtil.getInt();
        entity.setUpdateTime(now);
        Integer result = clueUserDao.update(entity);
        if (Objects.equals(1, result)) {
            PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_CLUE_TEAM, entity);
            InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            paasEsModel.update(saveDTO);
        }
        return result;
    }

    @Override
    public List<ClueUserEntity> findEntitys(Map<String, Object> param) {
        return clueUserDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return clueUserDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<UserTeamEntity> findBaseEntitys(Map<String, Object> map) {
        return clueUserDao.findBaseEntitys(map);
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del) throws XbbException {
        clueUserDao.batchUpdateByIdIn(corpid,idIn,isMain,del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(ClueTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        clueUserDao.batchUpdateByIdIn(corpid,idIn,isMain,del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(ClueTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);

        if (Objects.nonNull(refreshPolicy)) {
            paasEsModel.updateBatch(insertList, refreshPolicy);
        } else {
            paasEsModel.updateBatch(insertList);
        }
    }

    @Override
    public Integer updateBatch(List<ClueUserEntity> list, String corpid) throws XbbException{
        Integer result = clueUserDao.updateBatch(list, corpid);
        List<InsertDTO> insertList = new ArrayList<>();
        for (ClueUserEntity entity : list){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(entity.getDel())) {
                map.put("del", entity.getDel());
            }
            map.put("updateTime", DateTimeUtil.getInt());
            JSONObject object = new JSONObject();
            if (Objects.nonNull(entity.getIsMain())) {
                object.put(ClueTeamEnum.IS_MAIN.getDataAttr(), entity.getIsMain());
            }
            if (Objects.nonNull(entity.getDataId())) {
                object.put(ClueTeamEnum.DATA_ID.getDataAttr(), entity.getDataId());
            }
            if (Objects.nonNull(entity.getUserId())) {
                object.put(ClueTeamEnum.USER_ID.getDataAttr(), entity.getUserId());
            }
            if (Objects.nonNull(entity.getUserName())) {
                object.put(ClueTeamEnum.USER_NAME.getDataAttr(), entity.getUserName());
            }
            if (Objects.nonNull(entity.getUserAvatar())) {
                object.put(ClueTeamEnum.USER_AVATAR.getDataAttr(), entity.getUserAvatar());
            }
            if (Objects.nonNull(entity.getBackTime())) {
                object.put(ClueTeamEnum.BACK_TIME.getDataAttr(), entity.getBackTime());
            }
            if (Objects.nonNull(entity.getDistributionTime())) {
                object.put(ClueTeamEnum.DISTRIBUTION_TIME.getDataAttr(), entity.getDistributionTime());
            }
            map.put(StringConstant.JSON_DATA, object);
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);
        return result;
    }

    @Override
    public Integer deleteBatchByClueIdIn(List<Long> clueIdIn, String corpid) throws XbbException{
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",corpid);
        param.put("del",0);
        param.put("dataIdIn",clueIdIn);
        List<ClueUserEntity> clueUserEntities = findEntitys(param);
        Integer result = clueUserDao.deleteBatchByClueIdIn(clueIdIn, corpid);
        List<Long> removeIdList = new ArrayList<>();
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (ClueUserEntity entity : clueUserEntities){
            removeIdList.add(entity.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("del", 1);
            map.put(StringConstant.UPDATE_TIME, DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
        }
        clueUserDao.batchDeleteById(corpid,removeIdList, DelEnum.DELETE.getDel());
        paasEsModel.updateBatch(insertDTOList, WriteRequest.RefreshPolicy.NONE);
        return result;
    }

    @Override
    public void disbandedTeam(String corpid, List<Long> clueIdList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException{
        Map<String,Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid",corpid);
        params.put("del",-1);
        params.put("dataIdIn",clueIdList);
        List<ClueUserEntity> perClueUserEntityList = findEntitys(params);
        List<InsertDTO> preUpdateBatch = new ArrayList<>();
        List<Long> preIdList = new ArrayList<>();
        if (!perClueUserEntityList.isEmpty()) {
            for (ClueUserEntity entity : perClueUserEntityList) {
                preIdList.add(entity.getId());
                InsertDTO insertDTO = new InsertDTO();
                insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + entity.getId());
                insertDTO.setParent(corpid + "_" + entity.getDataId());
                Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                map.put("del", 1);
                map.put("updateTime", DateTimeUtil.getInt());
                JSONObject object = new JSONObject();
                object.put(ClueTeamEnum.DISTRIBUTION_TIME.getDataAttr(), DateTimeUtil.getInt());
                map.put(StringConstant.JSON_DATA, object);
                insertDTO.setSource(map);
                insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
                preUpdateBatch.add(insertDTO);
            }
            clueUserDao.batchDeleteById(corpid, preIdList, DelEnum.DELETE.getDel());
            if (Objects.nonNull(refreshPolicy)) {
                paasEsModel.updateBatch(preUpdateBatch, refreshPolicy);
            } else {
                paasEsModel.updateBatch(preUpdateBatch);
            }
        }
        params.put("del",0);
        List<ClueUserEntity> clueUserEntities = findEntitys(params);
        List<Long> idList = new ArrayList<>();
        List<InsertDTO> updateBatch = new ArrayList<>();
        for (ClueUserEntity entity : clueUserEntities){
            idList.add(entity.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("del", -1);
            map.put("updateTime", DateTimeUtil.getInt());
            JSONObject object = new JSONObject();
            object.put(ClueTeamEnum.DISTRIBUTION_TIME.getDataAttr(), DateTimeUtil.getInt());
            map.put(StringConstant.JSON_DATA, object);
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            updateBatch.add(insertDTO);
        }
        clueUserDao.disbandedTeam(corpid,idList);
        if (Objects.nonNull(refreshPolicy)) {
            paasEsModel.updateBatch(updateBatch, refreshPolicy);
        } else {
            paasEsModel.updateBatch(updateBatch);
        }
    }

    @Override
    public void batchDeleteTeam(String corpid, List<Long> clueIdIn, List<String> userIdList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException{
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("dataIdIn",clueIdIn);
        map.put("userIdIn",userIdList);
        List<ClueUserEntity> clueUserEntityList = findEntitys(map);
        clueUserDao.batchDelete(corpid,clueIdIn,userIdList);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (ClueUserEntity entity : clueUserEntityList){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> sourceMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            sourceMap.put(ParameterConstant.DEL, DelEnum.DELETE.getDel());
            insertDTO.setSource(sourceMap);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            insertDTOList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertDTOList, refreshPolicy);
    }

    @Override
    public void batchDeleteByClueId(String corpid, List<Long> clueIdIn, List<String> userIdList) throws XbbException{
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("dataIdIn",clueIdIn);
        map.put("userIdIn",userIdList);
        List<ClueUserEntity> clueUserEntityList = findEntitys(map);
        clueUserDao.batchDelete(corpid,clueIdIn,userIdList);
        for (ClueUserEntity entity : clueUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CLUE_TEAM,entity.getDataId());
        }
    }

    @Override
    public void batchUpgrade(String corpid, List<ClueUserEntity> clueUserEntities, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException{
        clueUserDao.batchUpgrade(corpid, clueUserEntities);
        List<InsertDTO> insertList = new ArrayList<>();
        for (ClueUserEntity entity : clueUserEntities){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            JSONObject object = new JSONObject();
            object.put(ClueTeamEnum.IS_MAIN.getDataAttr(), 1);
            map.put(StringConstant.JSON_DATA, object);
            map.put("updateTime", DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy);
    }

    @Override
    public void deleteBatch(String corpid, List<ClueUserEntity> clueUserEntities, WriteRequest.RefreshPolicy refreshPolicy, Integer del) throws XbbException {
        if (CollectionUtils.isEmpty(clueUserEntities)) {
            return;
        }
        List<Long> removeIdList = new ArrayList<>();
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (ClueUserEntity entity : clueUserEntities){
            removeIdList.add(entity.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("del", del);
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            insertDTOList.add(insertDTO);
        }
        clueUserDao.batchDeleteById(corpid,removeIdList, del);
        paasEsModel.updateBatch(insertDTOList, refreshPolicy);
    }

    @Override
    public List<String> getClueUserIdList(String corpid, Long clueId, Integer isMain) {
        return clueUserDao.getClueUserIdList(corpid, clueId, isMain);
    }

    @Override
    public List<ClueUserEntity> getClueUserList(String corpid, Long clueId, Integer isMain) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("dataId", clueId);
        param.put("isMain", isMain);
        return findEntitys(param);
    }

    @Override
    public List<String> getClueUserIds(String corpid, List<Long> clueIds, Integer isMain) {
        return clueUserDao.getClueUserIds(corpid, clueIds, isMain);
    }


    @Override
    public List<Long> getDataIdList4Rule(String corpid, Integer isMain, String userId, Long startId, Integer pageNum) {
        return clueUserDao.getDataIdList4Rule(corpid,isMain,userId, startId, pageNum);
    }

    /**
     * 更新协同人可编辑权限
     *
     * @param clueUserEntity
     */
    @Override
    public void updateEditFormPer(ClueUserEntity clueUserEntity) throws XbbException {
        updateFormTeam(clueUserEntity);
    }

    /**
     * 编辑团队成员标签
     *
     * @param clueUserEntity
     */
    @Override
    public void editLabel(ClueUserEntity clueUserEntity) throws XbbException {
        updateFormTeam(clueUserEntity);
    }

    /**
     * 修改线索名称
     *
     * @param clueUserEntity
     */
    @Override
    public void renameTeam(ClueUserEntity clueUserEntity) throws XbbException{
        updateFormTeam(clueUserEntity);
    }

    private void updateFormTeam(ClueUserEntity clueUserEntity) throws XbbException {
        Long updateTime = DateTimeUtil.getInt();
        clueUserEntity.setUpdateTime(updateTime);
        clueUserDao.update(clueUserEntity);
        InsertDTO insertDTO = new InsertDTO();
        String corpid = clueUserEntity.getCorpid();
        insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + clueUserEntity.getId());
        insertDTO.setParent(corpid + "_" + clueUserEntity.getDataId());
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("updateTime", updateTime);
        insertDTO.setSource(map);
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
        paasEsModel.update(insertDTO);
    }

    /**
     * 批量更新团队的人员为协同人身份以及协同人的可编辑权限
     *
     * @param corpid
     * @param dataIdIn
     * @param teamEntityList
     * @param isMain
     * @param needThrow
     * @throws XbbException
     */
    @Override
    public void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow) throws XbbException {
        batchUpdateCoAndPerByUserIdIn(corpid, idIn, teamEntityList, isMain, needThrow, WriteRequest.RefreshPolicy.WAIT_UNTIL);
    }

    @Override
    public void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        Integer formPermission = teamEntityList.get(BasicConstant.ZERO).getFormPermission();
        clueUserDao.batchUpdateCoAndPerByUserIdIn(corpid, idIn, isMain, formPermission);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(PaymentSheetTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy, needThrow);
    }

    /**
     * 批量更新团队中的协同人的负责人信息
     *
     * @param idIn 团队表中的主键id
     * @param updateUserTeamMainList 更新协同人的负责人信息
     */
    @Override
    public void batchUpdateTeamMainByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> updateUserTeamMainList) throws XbbException {
        batchUpdateTeamMainByUserIdIn(corpid, idIn, updateUserTeamMainList , WriteRequest.RefreshPolicy.NONE);

    }

    @Override
    public void batchUpdateTeamMainByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> updateUserTeamMainList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        Long updateTime = DateTimeUtil.getInt();
        clueUserDao.batchUpdateTeamMain(updateUserTeamMainList, updateTime);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : updateUserTeamMainList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CLUE_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("updateTime", updateTime);
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy);
    }
}
